package com.itheima;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kabuda
 * @create 2022-08-26 20:03
 */
public class testJasperReports {
    @Test
    public void testJasperReports()throws Exception{
        String jrxmlPath =
                "D:\\cheer\\Java\\SSM整合\\传智健康\\code\\itcast_health\\health-parent\\JasperReports\\src\\main\\resources\\demo.jrxml";
        String jasperPath =
                "D:\\cheer\\Java\\SSM整合\\传智健康\\code\\itcast_health\\health-parent\\JasperReports\\src\\main\\resources\\demo.jasper";

        //编译模板
        JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);

        //构造数据
        Map paramters = new HashMap();
        paramters.put("reportDate","2019-10-10");
        paramters.put("company","itcast");
        List<Map> list = new ArrayList();
        Map map1 = new HashMap();
        map1.put("name","xiaoming");
        map1.put("address","beijing");
        map1.put("email","xiaoming@itcast.cn");
        Map map2 = new HashMap();
        map2.put("name","xiaoli");
        map2.put("address","nanjing");
        map2.put("email","xiaoli@itcast.cn");
        list.add(map1);
        list.add(map2);

        //填充数据
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(jasperPath,
                        paramters,
                        new JRBeanCollectionDataSource(list));

        //输出文件
        String pdfPath = "D:\\test.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
    }

    @Test
    public void test2() throws Exception {
        //　　首先我们知道Class.forName() 方法要求JVM查找并加载指定的类到内存中，此时将"com.mysql.jdbc.Driver" 当做参数传入，就是告诉JVM，去"com.mysql.jdbc"这个路径下找Driver类，将其加载到内存中。
        //将mysql驱动注册到DriverManager中去
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/health?characterEncoding=utf-8&serverTimezone=UTC", "root", "kabuda");
        String jrxmlPath =
                "D:\\cheer\\Java\\SSM整合\\传智健康\\code\\itcast_health\\health-parent\\JasperReports\\src\\main\\resources\\demo1.jrxml";
        String jasperPath =
                "D:\\cheer\\Java\\SSM整合\\传智健康\\code\\itcast_health\\health-parent\\JasperReports\\src\\main\\resources\\demo1.jasper";
        //编译
        JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);

        //构造数据
        Map map=new HashMap();
        map.put("company","达布卡健康");

        //填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath, map, connection);

        //输出文件
        String pdfPath = "D:\\test.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
    }
    @Test
    public void test3() throws Exception {
        String jrxmlPath =
                "D:\\cheer\\Java\\SSM整合\\传智健康\\code\\itcast_health\\health-parent\\JasperReports\\src\\main\\resources\\demo2.jrxml";
        String jasperPath =
                "D:\\cheer\\Java\\SSM整合\\传智健康\\code\\itcast_health\\health-parent\\JasperReports\\src\\main\\resources\\demo2.jasper";
        //编译
        JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);

        //构造数据
        Map map=new HashMap();
        map.put("company","达布卡健康");

        //构造数据
        List<Map> list = new ArrayList();
        Map map1 = new HashMap();
        map1.put("name","入职体检套餐");
        map1.put("code","RZTJ");
        map1.put("age",18);
        map1.put("sex","男");

        Map map2 = new HashMap();
        map2.put("name","阳光爸妈老年健康体检");
        map2.put("code","YGBM");
        map2.put("age",18);
        map2.put("sex","女");
        list.add(map1);
        list.add(map2);

        //填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath, map,new JRBeanCollectionDataSource(list));

        //输出文件
        String pdfPath = "D:\\test.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
    }
}
